<template>
  <d2-container>
    <template slot="header" v-if="type === 'edit'">敞口管理-修改</template>
    <template slot="header" v-if="type === 'copy'">敞口管理-复制</template>
    <el-card shadow="never" class="d2-mb titleline">
      <template slot="header" style="height:66px;">
        <span class="title_x el-button--primary fl"></span>
        <p class="title_l fl">主要信息</p>
      </template>
      <el-form class="filter-container" ref="copy_ckxhForm" :model="copy_ckxhForm" :rules="rules" label-width="110px">
        <el-row :gutter="20">
          <el-col :span="4">
            <el-form-item label="部门名称" prop="department">
              <el-select v-model="copy_ckxhForm.department" placeholder="请选择" style="width:100%;">
                <el-option v-for="item in selectDepartsList" :key="item.department" :label="item.name"
                  :value="item.department">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="客户名称" prop="customer">
              <el-select v-model="copy_ckxhForm.customer" placeholder="请选择" style="width:100%;">
                <el-option v-for="item in customerList" :key="item.customer" :label="item.name" :value="item.customer">
                  <span style="float: left">{{item.name}}</span>
                  <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ item.customer}}</span> -->
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="币种" prop="currency">
              <el-select v-model="copy_ckxhForm.currency" placeholder="请选择" style="width:100%;">
                <el-option v-for="item in selectCurrencyList" :key="item.currency" :label="item.name"
                  :value="item.currency">
                  <span style="float: left">{{item.name}}</span>
                  <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ item.currency}}</span> -->
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="方向">
              <el-select clearable v-model="copy_ckxhForm.direction" class="filter-item" placeholder="请选择">
                <el-option label="采购" value="0"></el-option>
                <el-option label="销售" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="虚拟购销">
              <el-select clearable v-model="copy_ckxhForm.virtual" class="filter-item" placeholder="请选择">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="期初标识">
              <el-select clearable v-model="copy_ckxhForm.begin_flag" class="filter-item" placeholder="请选择">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card shadow="never" class="d2-mb titleline">
      <template slot="header" style="height:66px;">
        <span class="title_x el-button--primary fl"></span>
        <p class="title_l fl">详细信息</p>
      </template>
      <el-form label-position="right" status-icon :rules="rules" :model="edit_ckxhtableData" ref="ruleForm">
        <el-table :data="edit_ckxhtableData" ref="table" tooltip-effect="dark" border stripe style="width: 100%"
          @selection-change='selectRow' class="edit">
          <el-table-column type="selection" width="40" align="center"></el-table-column>
          <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
          <el-table-column label="敞口类型" align="center" width="110">
            <template slot-scope="scope">
              <el-select clearable v-model="scope.row.exp_type" placeholder="请选择" filterable allow-create>
                <el-option label="逐笔" value="0"></el-option>
                <el-option label="净敞口" value="1"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="定价方式" align="center" width="110">
            <template slot-scope="scope">
              <el-select clearable v-model="scope.row.pricing_method" placeholder="请选择" filterable allow-create
                @input="Bhx">
                <el-option label="一口价" value="0"></el-option>
                <el-option label="点价" value="1"></el-option>
                <el-option label="均价" value="2"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="合同号" align="center" width="110" v-if="type === 'copy'">
            <template slot-scope="scope">
              <el-form-item :prop="scope.$index + '.deal_no'" :rules="rules.deal_no">
                <el-input class="deal_no" v-model="scope.row.deal_no"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="敞口号" align="center" width="110" v-if="type === 'edit'">
            <template slot-scope="scope">
              <el-form-item :prop="scope.$index + '.exposure_no'" :rules="rules.exposure_no">
                <el-input class="exposure_no" v-model="scope.row.exposure_no"
                  :readonly="scope.row.pricing_method != '1'" disabled></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="品名" width="145">
            <template slot-scope="scope">
              <el-form-item class="dib" :prop="scope.$index + '.commodity'" :rules="rules.commodity">
                <el-input class="id dib" v-model="scope.row.name" style="width:60px;margin-right:5px;" disabled>
                </el-input>
                <el-input class="id dib" v-model="scope.row.commodity" style="width:60px;margin-right:5px;" v-if="a">
                </el-input>
              </el-form-item>
              <el-button type="primary" class="dib" size="mini" plain @click="PMdialog(scope,$index,scope.row)">选择
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="仓库" align="center" width="110">
            <template slot-scope="scope">
              <el-form-item :prop="scope.$index + '.depot'" :rules="rules.depot">
                <el-input class="depot" v-model="scope.row.depot"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="单据数量" align="center" width="110">
            <template slot-scope="scope">
              <el-form-item :prop="scope.$index + '.invoice_vol'" :rules="rules.invoice_vol">
                <el-input v-model="scope.row.invoice_vol" type="number" min="1"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="敞口数量" align="center" width="110">
            <template slot-scope="scope">
              <el-form-item :prop="scope.$index + '.exp_vol'" :rules="rules.exp_vol">
                <el-input v-model="scope.row.exp_vol" type="number"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="定价开始日期" width="161" sortable class="date">
            <template slot-scope="scope">
              <el-form-item :prop="scope.$index + '.start_date'" :rules="rules.start_date">
                <el-date-picker v-model="scope.row.start_date" v-show="scope.row.pricing_method == '0'"
                  :picker-options="pickerOptions" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                  style="width:100%;padding-right:0;"></el-date-picker>
                <el-date-picker v-model="scope.row.start_date"
                  v-show="scope.row.pricing_method == '1'||scope.row.pricing_method == '2'" type="date"
                  value-format="yyyy-MM-dd" placeholder="选择日期" style="width:100%;padding-right:0;"></el-date-picker>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="基准价格" align="center" width="170">
            <template slot-scope="scope">
              <el-input class="id dib" v-model="scope.row.base_price_name" style="width:88px;margin-right:5px;"
                disabled></el-input>
              <el-input class="id dib" v-model="scope.row.base_price" style="width:88px;margin-right:5px;" v-if="a">
              </el-input>
              <el-button type="primary" size="mini" plain @click="DJdialog(scope,$index,scope.row)">选择</el-button>
            </template>
          </el-table-column>
          <el-table-column label="汇率" align="center" width="90">
            <template slot-scope="scope">
              <el-form-item :prop="scope.$index + '.exchange_rate'" :rules="rules.exchange_rate">
                <el-input v-model="scope.row.exchange_rate" type="number"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="单价" align="center" width="90">
            <template slot-scope="scope">
              <el-input v-model="scope.row.unit_price" type="number" :readonly="scope.row.pricing_method == '2'">
              </el-input>
            </template>
          </el-table-column>
          <el-table-column label="升贴水" align="center" width="90">
            <template slot-scope="scope">
              <el-input v-model="scope.row.bwd" type="number"
                :readonly="scope.row.pricing_method == '0' || scope.row.pricing_method == '2'"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="价格" align="center" width="90">
            <template slot-scope="scope">
              <el-input v-model="scope.row.price" type="number" disabled></el-input>
            </template>
          </el-table-column>
          <el-table-column label="本币价格" align="center" width="90">
            <template slot-scope="scope">
              <el-input v-model="scope.row.currency_price" type="number" disabled></el-input>
            </template>
          </el-table-column>
          <el-table-column label="件数" align="center" width="90">
            <template slot-scope="scope">
              <el-form-item :prop="scope.$index + '.piece_num'" :rules="rules.piece_num">
                <el-input v-model="scope.row.piece_num" type="number" :min="0"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="每件数量" align="center" width="90">
            <template slot-scope="scope">
              <el-input v-model="scope.row.piece_vol" type="number" :min="0"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="定价结束日期" width="161" sortable class="date">
            <template slot-scope="scope">
              <el-form-item :prop="scope.$index + '.end_date'" :rules="rules.end_date">
                <el-date-picker v-model="scope.row.start_date" v-if="scope.row.pricing_method == '0'" type="date"
                  value-format="yyyy-MM-dd" :readonly="scope.row.pricing_method == '0'" placeholder="选择日期"
                  style="width:100%;padding-right:0;"></el-date-picker>
                <el-date-picker v-model="scope.row.end_date" type="date" v-else value-format="yyyy-MM-dd" placeholder="选择日期"
                  :readonly="scope.row.pricing_method == '0'" style="width:100%;padding-right:0;"></el-date-picker>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center">
            <template slot-scope="scope">
              <el-input class="remark" v-model="scope.row.remark"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </el-card>
    <el-button type="primary" size="mini" style="float:right;margin-left: 10px;" v-if="type === 'copy'"
      @click="CopyChange('copy_ckxhForm','ruleForm')">计算并保存</el-button>
    <el-button type="primary" size="mini" style="float:right;margin-left: 10px;" v-if="type === 'edit'"
      @click="CkglChange('copy_ckxhForm','ruleForm')">计算并保存</el-button>
    <!-- <el-button type="default" size="mini" style="float:right">取消</el-button> -->
    <!-- 品名 -->
    <el-dialog :visible.sync="PMdialogVisible" title="品名" width="74%">
      <el-table :data="PMNameList" border highlight-current-row @current-change="handleCurrentChange_ta">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="status" label="状态" sortable :formatter="PMStatus"></el-table-column>
        <el-table-column prop="category" label="品类" sortable></el-table-column>
        <el-table-column prop="commodity" label="品名" sortable></el-table-column>
        <el-table-column prop="id" label="品名ID" sortable></el-table-column>
        <el-table-column prop="commodity_unit" label="商品单位" sortable :formatter="SPFormat"></el-table-column>
        <el-table-column prop="measure_unit" label="计量单位" sortable :formatter="JLFormat"></el-table-column>
        <el-table-column prop="tax_typeName" label="税种" sortable></el-table-column>
        <el-table-column prop="start_date" label="开始日期" sortable></el-table-column>
        <el-table-column prop="rate" label="税率" sortable></el-table-column>
        <el-table-column prop="brand" label="品牌" sortable></el-table-column>
        <el-table-column prop="origin" label="产地" sortable></el-table-column>
        <el-table-column prop="standard" label="规格" sortable></el-table-column>
        <el-table-column prop="attribute" label="主属性" sortable></el-table-column>
        <el-table-column prop="desc" label="备注" sortable></el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="PMdialogVisible = false">取消</el-button>
        <el-button type="primary" @click="PMIDChange">保存</el-button>
      </div>
    </el-dialog>
    <!-- 定价基准 -->
    <el-dialog :visible.sync="DjjzdialogVisible" title="价格项-定价基准" width="60%">
      <el-table :data="DjjzNameList" border highlight-current-row @current-change="handleCurrentChange_ta">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="status" label="状态" sortable :formatter="formatterStatus"></el-table-column>
        <el-table-column prop="id" label="价格项ID" sortable></el-table-column>
        <el-table-column prop="name" label="价格项名称" sortable></el-table-column>
        <el-table-column prop="price_origin" label="价格项来源" sortable :formatter="price_originFormat"></el-table-column>
        <el-table-column prop="price_unit_name" label="单位" sortable :formatter="DWFormat"></el-table-column>
        <el-table-column prop="price_formula" label="定价公式" sortable></el-table-column>
        <el-table-column prop="desc" label="备注" sortable></el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="DjjzdialogVisible = false">取消</el-button>
        <el-button type="primary" @click="DjjzChange">保存</el-button>
      </div>
    </el-dialog>
    <!-- 估值基准 -->
    <el-dialog :visible.sync="GzjzdialogVisible" title="价格项-估值基准" width="60%">
      <el-table :data="GzjzNameList" border highlight-current-row @current-change="handleCurrentChange_ta">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="status" label="状态" sortable :formatter="formatterStatus"></el-table-column>
        <el-table-column prop="id" label="价格项ID" sortable></el-table-column>
        <el-table-column prop="name" label="价格项名称" sortable></el-table-column>
        <el-table-column prop="price_origin" label="价格项来源" sortable :formatter="price_originFormat"></el-table-column>
        <el-table-column prop="price_unit_name" label="单位" sortable :formatter="DWFormat"></el-table-column>
        <el-table-column prop="price_formula" label="定价公式" sortable></el-table-column>
        <el-table-column prop="desc" label="备注" sortable></el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="GzjzdialogVisible = false">取消</el-button>
        <el-button type="primary" @click="GZIDChange">保存</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
  import {
    concustomer,
    addExposure,
    conCommodity,
    conPriceitem,
    conCurrency,
    calculate
  } from '../../../../../api/url_lcc';
  import {
    getDepartments
  } from '../../../../../api/url';
  export default {
    //现货期货套期-新增/编辑
    name: "edit_open",
    data() {
      return {
        a: false,
        selectDepartsList: [],
        customerList: [],
        edit_ckxhtableData: [],
        // {
        //  "deal_no":"",
        //  "base_price":"",
        //  "base_valuation":"",
        //  "bwd": "",
        //  "commodity": "",
        //  "confirm_status": "",
        //  "currency_price": "",
        //  "deal_no": "",
        //  "depot": "",
        //  "end_date": "",
        //  "exchange_rate": "",
        //  "exp_vol": "",
        //  "exp_type": "",
        //  "invoice_vol": "",
        //  "piece_num":  "",
        //  "piece_vol":  "",
        //  "price": "",
        //  "red_dashed": "",
        //  "remark": "",
        //  "settlement_status": "",
        //  "start_date":  "",
        //  "unit_price":  "",
        //  "pricing_method":  "",
        // },
        copy_ckxhForm: {
          "currency": "",
          "direction": "",
          "pricing_method": "",
          "virtual": "",
          "begin_flag": "",
        },
        GzjzNameList: [],
        DjjzNameList: [],
        type: '',
        PMNameList: [],
        selectCurrencyList: [],
        selectlistRow: [],
        rowNum: 1,
        PMdialogVisible: false,
        DjjzdialogVisible: false,
        GzjzdialogVisible: false,
        row: "",
        value: "",
        ruleForm: {
          "deal_no": "",
          "commodity": "",
          "depot": "",
          "invoice_vol": "",
          "exp_vol": "",
          "start_date": "",
          "base_price": "",
          "base_valuation": "",
          "exchange_rate": "",
          "unit_price": "",
          "bwd": "",
          "price": "",
          "currency_price": "",
          "piece_num": "",
          "piece_vol": "",
          "end_date": "",
          //            "remark": ""
        },
        // ///////////////////////////////时间限制
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now(); //选择时间范围
          }
        },
        pickerOptionsStart: {
          disabledDate: time => {
            let endDateVal = this.CKForm.endDate;
            if (endDateVal) {
              return time.getTime() > new Date(endDateVal).getTime();
            }
          }
        },
        pickerOptionsEnd: {
          disabledDate: time => {
            let beginDateVal = this.CKForm.startDate;
            if (beginDateVal) {
              return (
                time.getTime() <
                new Date(beginDateVal).getTime() - 1 * 24 * 60 * 60 * 1000
              );
            }
          }
        },
        rules: {
          department: [{
            required: true,
            message: '请选择部门名称',
            trigger: 'change'
          }],
          customer: [{
            required: true,
            message: '请选择客户名称',
            trigger: 'change'
          }],
          currency: [{
            required: true,
            message: '请选择币种',
            trigger: 'change'
          }],
          deal_no: [{
            required: true,
            message: '请输入合同号',
            trigger: 'blur'
          }, ],
          commodity: [{
            required: true,
            message: '请选择品名',
            trigger: 'blur'
          }, ],
          depot: [{
            required: true,
            message: '请输入仓库',
            trigger: 'blur'
          }, ],
          invoice_vol: [{
            required: true,
            message: '请输入单据数量',
            trigger: 'blur'
          }, ],
          start_date: [{
            type: 'string',
            required: true,
            message: '请选择定价开始日期',
            trigger: 'change'
          }],
          exchange_rate: [{
            required: true,
            message: '请输入汇率',
            trigger: 'blur'
          }, ],
          //  piece_num: [
          //   { required: true, message: '请输入件数', trigger: 'blur' },
          // ],
          //  piece_vol: [
          //   { required: true, message: '请输入每件数量', trigger: 'blur' },
          // ],
          //  remark: [
          //   { required: true, message: '请输入备注', trigger: 'blur' },
          // ],
        }
      };
    },
    created() {
      this.tabledatas.map(i => {
        i.show = false
        return i
      })
    },
    methods: {
      formatterStatus(row, column) {
        return row.status == 0 ? '停用' : '启用'
      },
      price_originFormat(row, column) {
        if (row.price_origin === 0) {
          return '现货行情';
        } else if (row.price_origin === "1") {
          return '期货主力合约行情';
        } else if (row.price_origin === "2") {
          return '期货合约行情';
        } else if (row.price_origin === "3") {
          return '计算项';
        } else if (row.price_origin === "4") {
          return '用户现货行情';
        }
      },
      DWFormat(row, column) {
        if (row.price_unit === 0) {
          return '吨';
        } else if (row.price_unit === 1) {
          return '千克';
        } else if (row.price_unit === 2) {
          return '桶';
        } else if (row.price_unit === 3) {
          return '立方米';
        } else if (row.price_unit === 4) {
          return '张';
        } else if (row.price_unit === 5) {
          return '克';
        }
      },
      //  部门信息
      //树形结构转换为扁平数组
      treeToList(tree) {
        var queen = [];
        var out = [];
        queen = queen.concat(tree);
        while (queen.length) {
          var first = queen.shift();
          if (first.sub_depart) {
            queen = queen.concat(first.sub_depart);
            delete first["sub_depart"];
          }
          out.push(first);
        }
        return out;
      },
      // 查询部门
      handle_getDepartments() {
        getDepartments()
          .then(res => {
            this.selectDepartsList = [];
            var data1 = res.result;
            var arr = this.treeToList(data1);
            for (var i = 0; i < arr.length; i++) {
              this.selectDepartsList[i] = {
                name: arr[i]["name"],
                department: arr[i]["id"]
              };
            }
          })
          .catch(err => {
            this.$message.error(err.msg);
          });
      },
      //  客商信息
      handle_customerChange() {
        concustomer(this.cusForm).then(res => {
          this.customerList = []
          var data = res.result
          for (var i = 0; i < data.length; i++) {
            this.customerList[i] = {
              'customer': data[i].id,
              'name': data[i].name
            }
          }
          console.log(1, data, 2, this.customerList)
        }).catch(err => {
          this.$message.error(err.msg);
        });
      },
      // 品名
      PMNameChange() {
        let params = {
          "category": "",
          "name": "",
          "commodity_spot__brand": "",
          "status": "",
        };
        conCommodity(params).then(res => {
          this.PMNameList = []
          var data = res.result
          for (var i = 0; i < data.length; i++) {
            this.PMNameList[i] = {
              'status': data[i].tax_type['status'],
              'category': data[i]['category'],
              'commodity': data[i]['name'],
              'id': data[i]['id'],
              'commodity_unit': data[i]['commodity_unit'],
              'measure_unit': data[i]['measure_unit'],
              'tax_typeName': data[i].tax_type['name'],
              'start_date': data[i].tax_type['start_date'],
              'rate': data[i].tax_type['rate'],
              'brand': data[i].commodity_spot[0]['brand'],
              'origin': data[i].commodity_spot[0]['origin'],
              'standard': data[i].commodity_spot[0]['standard'],
              'attribute': data[i].commodity_spot[0]['attribute'],
              'desc': data[i]['desc']
            }
          }
          console.log('品名', this.PMNameList)
        }).catch(err => {
          this.$message.error(err.msg);
        });
      },
      PMdialog(index, row) {
        this.PMdialogVisible = true
        this.PMNameChange();
      },
      // 品名选中id
      PMIDChange(index, row) {
        this.PMdialogVisible = false
        console.log('品名', this.currentRow)
        this.edit_ckxhtableData[0].commodity = this.currentRow.id
        this.edit_ckxhtableData[0].name = this.currentRow.commodity
        console.log(14, this.edit_ckxhtableData[0].name)
      },
      // // 估值基准
      // GzjzNameChange(){
      //   let params = {
      //       "name": "",
      //       "status":"",
      //       "price_origin": ""
      //   };
      //   conPriceitem(params).then(res => {
      //           this.GzjzNameList = []
      //           var data = res.result
      //           console.log(12,data)
      //           for (var i = 0; i < data.length; i++) {
      //             this.GzjzNameList[i] = {
      //                 'status': data[i]['status'],
      //                 'id': data[i]['id'],
      //                 'name': data[i]['name'],
      //                 "price_origin": data[i]['price_origin'],
      //                 "price_unit": data[i]['price_unit'],
      //                 "price_formula": data[i]['price_formula'],
      //                 "desc": data[i]['desc']
      //             }
      //         }
      //           console.log('品名',this.PMNameList)
      //       }).catch(err =>{
      //     this.$message.error(err.msg);
      //   });
      //   },
      // // 估值弹框
      // GZdialog(index,row){
      //   this.GzjzdialogVisible = true
      //   this.GzjzNameChange();
      // },
      // // 估值选中id
      // GZIDChange(index,row){
      //   this.GzjzdialogVisible = false
      //   console.log('估值/定价',this.currentRow)
      //   console.log('1',this.currentRow.id,2,this.edit_ckxhtableData[0].commodity)
      //   this.edit_ckxhtableData[0].base_valuation = this.currentRow.id
      //   console.log('1',this.edit_ckxhtableData[0].base_valuation)
      // },
      // 定价基准
      DjjzNameChange() {
        let params = {
          "name": "",
          "status": "",
          "price_origin": ""
        };
        conPriceitem(params).then(res => {
          this.DjjzNameList = []
          var data = res.result
          for (var i = 0; i < data.length; i++) {
            this.DjjzNameList[i] = {
              'status': data[i]['status'],
              'id': data[i]['id'],
              'name': data[i]['name'],
              "price_origin": data[i]['price_origin'],
              "price_unit": data[i]['price_unit'],
              "price_unit_name": data[i]['price_unit_name'],
              "price_formula": data[i]['price_formula'],
              "desc": data[i]['desc']
            }
          }
        }).catch(err => {
          this.$message.error(err.msg);
        });
      },
      DJdialog(index, row) {
        this.DjjzdialogVisible = true
        this.DjjzNameChange();
      },
      // 定价选中id
      DjjzChange(index, row) {
        this.DjjzdialogVisible = false
        console.log('定价', this.currentRow)
        this.edit_ckxhtableData[0].base_price = this.currentRow.id
        this.edit_ckxhtableData[0].base_price_name = this.currentRow.name
        console.log(142, this.edit_ckxhtableData[0].base_price_name)
      },

      // 查询币种
      handle_currencyChange() {
        let params = {
          "name": "",
        };
        conCurrency(params).then(res => {
          this.selectCurrencyList = []
          var data1 = res.result
          console.log(902, )
          for (var i = 0; i < data1.length; i++) {
            this.selectCurrencyList[i] = {
              currency: data1[i]['id'],
              name: data1[i]['name']
            };
          }
        }).catch(err => {
          this.$message.error(err.msg);
        });
      },

      // 行内编辑
      handleCellDataChange({
        rowIndex,
        key,
        value,
        row
      }) {
        debbuger;
        console.log(rowIndex);
        console.log(key);
        console.log(value);
        console.log(row);
      },

      // 单选
      handleCurrentChange(currentRow, oldCurrentRow) {
        console.log(currentRow);
        console.log(oldCurrentRow);
      },
      handleCurrentChange_ta(val) {
        this.currentRow = val;
        console.log('品名单选', val.id)
      },
      // 多选
      handleSelectionChange(selection) {
        console.log(selection);
      },
      // 获取传过来的参数
      editListAll() {
        this.edit_ckxhtableData = this.$route.params.editList
        this.edit_ckxhtableData.commodity = this.$route.params.editList[0].commodity.id
        this.edit_ckxhtableData.commodity_name = this.$route.params.editList[0].name
        this.type = this.$route.params.type
        this.copy_ckxhForm = {
          department: this.$route.params.editList[0].department,
          customer: this.$route.params.editList[0].customer,
          currency: this.$route.params.editList[0].currency,
          direction: this.$route.params.editList[0].direction,
          virtual: this.$route.params.editList[0].virtual,
          begin_flag: this.$route.params.editList[0].begin_flag,
        }
        console.log(12, this.edit_ckxhtableData[0].exp_type === 1)
        if (this.edit_ckxhtableData[0].exp_type === 1) {
          this.edit_ckxhtableData[0].exp_type === '净敞口'
        } else {
          this.edit_ckxhtableData[0].exp_type === '逐笔'
        }
      },
      Bhx() {
        if (this.edit_ckxhtableData[0].pricing_method === '0' || this.edit_ckxhtableData[0].pricing_method === '2') {
          this.edit_ckxhtableData[0].bwd = 0
        } else {
          this.edit_ckxhtableData[0].bwd = this.edit_ckxhtableData[0].bwd
        }
        if (this.edit_ckxhtableData[0].pricing_method === '2') {
          this.edit_ckxhtableData[0].unit_price = 0
        } else {
          this.edit_ckxhtableData[0].unit_price = this.edit_ckxhtableData[0].unit_price
        }
      },
      // 修改计算并保存-敞口管理
      CkglChange(copy_ckxhForm, ruleForm) {
        this.edit_ckxhtableData.forEach(item => {
          console.log(90, item.pricing_method != '0', 0, item);
          if (item.pricing_method == '0') {
            item.end_date = item.start_date
          } else {
            item.end_date = item.end_date
          }
        });
        const rule1 = new Promise((resolve, reject) => {
          this.$refs[copy_ckxhForm].validate(valid => {
            if (valid) {
              resolve();
            } else {
              console.log("error submit!!");
              return false;
            }
          });
        });
        const rule2 = new Promise((resolve, reject) => {
          this.$refs[ruleForm].validate(valid => {
            if (valid) {
              resolve();
            } else {
              console.log("error submit!!");
              return false;
            }
          });
        });
        console.log(211,this.edit_ckxhtableData)
        if (this.edit_ckxhtableData[0].pricing_method != '0' && this.edit_ckxhtableData[0].end_date == '' || this
          .edit_ckxhtableData[0].end_date == null) {
          this.$message.error('定价结束日期不能为空，请输入定价结束日期！')
        } else {
          Promise.all([rule1, rule2]).then(() => {
            let params = {
              operation: 'EXP',
              exp: this.edit_ckxhtableData
            };
            calculate(params).then(res => {
              console.log('敞口新增2', res)
              this.$message.success(res.result);
              var edit_ckxhtableData = []
              var data1 = res.result
              for (var i = 0; i < data1.length; i++) {
                edit_ckxhtableData[i] = {
                  "pricing_method": data1[i].pricing_method,
                  "exp_type": data1[i].exp_type,
                  "deal_no": data1[i].deal_no,
                  "commodity": data1[i].commodity,
                  "depot": data1[i].depot,
                  "invoice_vol": data1[i].invoice_vol,
                  "exp_vol": data1[i].new_exp_vol,
                  "start_date": data1[i].start_date,
                  "base_price": data1[i].base_price,
                  "base_valuation": data1[i].base_valuation,
                  "exchange_rate": data1[i].exchange_rate || "0",
                  "unit_price": data1[i].unit_price || "0",
                  //  "price_unit_name": data[i]['price_unit_name'],
                  "bwd": data1[i].bwd || "0",
                  "price": data1[i].new_price || "0",
                  "currency_price": data1[i].new_currency_price || "0",
                  "piece_num": data1[i].piece_num || "0",
                  "piece_vol": data1[i].piece_vol || "0",
                  "end_date": data1[i].end_date,
                  "exposure_no": data1[i].exposure_no,
                  "remark": data1[i].remark,
                }
              }
              this.edit_ckxhtableData = edit_ckxhtableData
              this.CkglChange1();
            }).catch(err => {
              this.$message.error(err.msg);
            });
          });
        }

      },
      // 敞口管理
      CkglChange1() {
        console.log('编辑源数据', this.edit_ckxhtableData)
        let params = {
          operation: "MOD",
          "spot_exposure": {
            "red_dashed": "0",
            "confirm_status": "0",
            "settlement_status": "0",
            "virtual": this.copy_ckxhForm.virtual,
            "begin_flag": this.copy_ckxhForm.begin_flag,
            "exp_type": this.edit_ckxhtableData[0].exp_type,
            "exposure_no": this.edit_ckxhtableData[0].exposure_no,
            "direction": this.copy_ckxhForm.direction,
            "depot": this.edit_ckxhtableData[0].depot,
            "exp_vol": this.edit_ckxhtableData[0].exp_vol,
            "invoice_vol": this.edit_ckxhtableData[0].invoice_vol,
            "unit_price": this.edit_ckxhtableData[0].unit_price,
            "bwd": this.edit_ckxhtableData[0].bwd,
            "currency": this.copy_ckxhForm.currency,
            "pricing_method": this.edit_ckxhtableData[0].pricing_method,
            "price": this.edit_ckxhtableData[0].price,
            "currency_price": this.edit_ckxhtableData[0].currency_price,
            "start_date": this.edit_ckxhtableData[0].start_date,
            "end_date": this.edit_ckxhtableData[0].end_date,
            "exchange_rate": this.edit_ckxhtableData[0].exchange_rate,
            "piece_num": this.edit_ckxhtableData[0].piece_num,
            "piece_vol": this.edit_ckxhtableData[0].piece_vol,
            "remark": this.edit_ckxhtableData[0].remark,
            "deal_no": this.edit_ckxhtableData[0].deal_no,
            "department": this.copy_ckxhForm.department,
            "customer": this.copy_ckxhForm.customer,
            "commodity": this.edit_ckxhtableData[0].commodity,
            "base_price": this.edit_ckxhtableData[0].base_price,
            // "base_valuation": this.edit_ckxhtableData[0].base_valuation,
          }
        };
        addExposure(params).then(res => {
          this.$message.success(res.result);
          this.$router.push('/arbitrage_tool/cash_management');
          this.closeing.closePage(this.$store.getters["d2admin/page/allPage"], this.$store.getters[
            "d2admin/page/currentPage"])
        }).catch(err => {
          this.$message.error(err.msg);
        });
      },
      // 复制计算并保存-敞口管理
      CopyChange(copy_ckxhForm, ruleForm) {
        this.edit_ckxhtableData.forEach(item => {
          console.log(90, item.pricing_method != '0', 0, item);
          if (item.pricing_method == '0') {
            item.end_date = item.start_date
          } else {
            item.end_date = item.end_date
          }
        });
        const rule1 = new Promise((resolve, reject) => {
          this.$refs[copy_ckxhForm].validate(valid => {
            if (valid) {
              resolve();
            } else {
              console.log("error submit!!");
              return false;
            }
          });
        });
        const rule2 = new Promise((resolve, reject) => {
          this.$refs[ruleForm].validate(valid => {
            if (valid) {
              resolve();
            } else {
              console.log("error submit!!");
              return false;
            }
          });
        });
        if (this.edit_ckxhtableData[0].pricing_method != '0' && this.edit_ckxhtableData[0].end_date == '' || this
          .edit_ckxhtableData[0].end_date == null) {
          this.$message.error('定价结束日期不能为空，请输入定价结束日期！')
        } else {
          Promise.all([rule1, rule2]).then(() => {
            let params = {
              operation: 'EXP',
              exp: this.edit_ckxhtableData
            };
            calculate(params).then(res => {
              // this.$message.success(res.result);
              var edit_ckxhtableData = []
              var data1 = res.result
              for (var i = 0; i < data1.length; i++) {
                edit_ckxhtableData[i] = {
                  "pricing_method": data1[i].pricing_method,
                  "exp_type": data1[i].exp_type,
                  "deal_no": data1[i].deal_no,
                  "commodity": data1[i].commodity,
                  "depot": data1[i].depot,
                  "invoice_vol": data1[i].invoice_vol,
                  "exp_vol": data1[i].new_exp_vol,
                  "start_date": data1[i].start_date,
                  "base_price": data1[i].base_price,
                  //                "base_valuation": data1[i].base_valuation,
                  "exchange_rate": data1[i].exchange_rate || "0",
                  "unit_price": data1[i].unit_price || "0",
                  "bwd": data1[i].bwd || "0",
                  "price": data1[i].new_price || "0",
                  "currency_price": data1[i].new_currency_price || "0",
                  "piece_num": data1[i].piece_num || "0",
                  "piece_vol": data1[i].piece_vol || "0",
                  "end_date": data1[i].end_date,
                  "exposure_no": data1[i].exposure_no,
                  "remark": data1[i].remark,
                }
              }
              this.edit_ckxhtableData = edit_ckxhtableData
              this.CopyChange1();
              this.$router.push('/arbitrage_tool/cash_management');
            }).catch(err => {
              this.$message.error(err.msg);
            });
          });
        }
      },
      // 复制-敞口管理
      CopyChange1() {
        // console.log("复制",this.edit_ckxhtableData,9,this.$route.params.editList)
        // if(this.edit_ckxhtableData[0].deal_no === this.$route.params.editList[0].deal_no){
        //   this.$message.error("添加现货敞口失败同一合同敞口数量总和不能大于单据数量!!!");
        // }else{
        this.edit_ckxhtableData.forEach(item => {
          console.log(90, item.pricing_method != '0', 0, item.end_date == '');
          if (item.pricing_method == '0') {
            item.end_date = item.start_date
          } else if (item.pricing_method != '0' && item.end_date == '') {
            this.$message.error('定价结束日期不能为空，请输入定价结束日期！')
          } else {
            item.end_date = item.end_date
          }
        });
        let params = {
          operation: "ADD",
          main: {
            department: this.copy_ckxhForm.department || "",
            customer: this.copy_ckxhForm.customer || "",
            currency: this.copy_ckxhForm.currency || "",
            direction: this.copy_ckxhForm.direction || "",
            virtual: this.copy_ckxhForm.virtual || "",
            begin_flag: this.copy_ckxhForm.begin_flag || ""
          },
          "spot_exposure": [{
            "base_price": this.edit_ckxhtableData[0].base_price,
            "base_valuation": this.edit_ckxhtableData[0].base_valuation,
            "bwd": this.edit_ckxhtableData[0].bwd,
            "commodity": this.edit_ckxhtableData[0].commodity,
            "confirm_status": this.edit_ckxhtableData[0].confirm_status,
            "currency_price": this.edit_ckxhtableData[0].currency_price,
            // "customer": this.edit_ckxhtableData[0].customer,
            "deal_no": this.edit_ckxhtableData[0].deal_no,
            // "department": this.edit_ckxhtableData[0].department,
            // "direction": this.edit_ckxhtableData[0].direction,
            "depot": this.edit_ckxhtableData[0].depot,
            "end_date": this.edit_ckxhtableData[0].end_date,
            "exchange_rate": this.edit_ckxhtableData[0].exchange_rate,
            "exp_vol": this.edit_ckxhtableData[0].exp_vol,
            "exp_type": this.edit_ckxhtableData[0].exp_type,
            // "exposure_no": this.edit_ckxhtableData[0].exposure_no,
            "invoice_vol": this.edit_ckxhtableData[0].invoice_vol,
            "piece_num": this.edit_ckxhtableData[0].piece_num,
            "piece_vol": this.edit_ckxhtableData[0].piece_vol,
            "price": this.edit_ckxhtableData[0].price,
            "red_dashed": this.edit_ckxhtableData[0].red_dashed,
            "remark": this.edit_ckxhtableData[0].remark,
            "settlement_status": this.edit_ckxhtableData[0].settlement_status,
            "start_date": this.edit_ckxhtableData[0].start_date,
            "unit_price": this.edit_ckxhtableData[0].unit_price,
            // "virtual": this.edit_ckxhtableData[0].virtual,
            // "begin_flag": this.edit_ckxhtableData[0].begin_flag,
            // "currency": this.edit_ckxhtableData[0].currency,
            "pricing_method": this.edit_ckxhtableData[0].pricing_method,
          }]
        };
        addExposure(params).then(res => {
          console.log('复制保存baocuo ', res)
          this.$message.success(res.result);
          //  this.$router.push('/arbitrage_tool/cash_management');
          //  this.closeing.closePage(this.$store.getters["d2admin/page/allPage"],this.$store.getters["d2admin/page/currentPage"])
        }).catch(err => {
          this.$message.error(err.msg);
        });
        // }

      },

      // Ckgl_handerChange(){
      //   if(this.$route.params.type === 'copy'){
      //     this.CopyChange()
      //   }else if(this.$route.params.type === 'edit'){
      //       this.CkglChange()
      //   }
      // }


    },
    mounted() {
      //   回显
      this.editListAll();
      this.handle_currencyChange();
      // this.PMIDChange()
      // this.GzjzNameChange()
      this.handle_getDepartments();
      this.handle_customerChange();
    }
  };

</script>
